package com.navinfo.opentsp.qingqi.trackoptimize.handler;


import com.navinfo.opentsp.qingqi.trackoptimize.commands.QueryTripPbPageInfoCommand;
import com.navinfo.opentsp.qingqi.trackoptimize.common.handler.BaseAbstractHandler;
import com.navinfo.opentsp.qingqi.trackoptimize.common.result.HttpCommandResultWithData;
import com.navinfo.opentsp.qingqi.trackoptimize.common.result.ReturnCode;
import com.navinfo.opentsp.qingqi.trackoptimize.service.TripQueryService;
import com.navinfo.opentsp.qingqi.trackoptimize.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 查询行程（分页）handler
 */
@Component
public class QueryTripPbPageInfoHandler extends BaseAbstractHandler<QueryTripPbPageInfoCommand, HttpCommandResultWithData> {

    protected static final Logger logger = LoggerFactory.getLogger(QueryTripPbPageInfoHandler.class);

    @Autowired
    private TripQueryService tripQueryService;

    public QueryTripPbPageInfoHandler() {
        super(QueryTripPbPageInfoCommand.class, HttpCommandResultWithData.class);
    }

    protected QueryTripPbPageInfoHandler(Class<QueryTripPbPageInfoCommand> commandType, Class<HttpCommandResultWithData> resultType) {
        super(commandType, resultType);
    }

    public HttpCommandResultWithData businessHandle(QueryTripPbPageInfoCommand command) {
        logger.info("businessHandle start");
        logger.info(command.toString());
        // 设置返回类型
        HttpCommandResultWithData result = new HttpCommandResultWithData();
        try {
            if (CheckUtil.dateCheckCompare(command.getBeginDate(), command.getEndDate())) {
                result.setResultCode(ReturnCode.CLIENT_ERROR.code());
                result.setMessage("开始时间不能大于结束时间");
                return result;
            }
            if (CheckUtil.dateCheckRange(command.getBeginDate(), command.getEndDate())) {
                result.setResultCode(ReturnCode.CLIENT_ERROR.code());
                result.setMessage("开始时间和结束时间跨度不能超过3个月");
                return result;
            }
            result.setData(tripQueryService.queryPageTrip(command));
            result.fillResult(ReturnCode.OK);
        } catch (Exception e) {
            result.fillResult(ReturnCode.SERVER_ERROR);
            logger.error("businessHandle ", e);
        }
        logger.info("businessHandle end");
        return result;
    }

}
